package com.zjeav.lingjiao.base.utils;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.view.Display;
import android.view.WindowManager;

/**
 * Created by li on 2018/5/15.
 */

public class PermissionUtils {
    // 提示用户去应用设置界面手动开启权限
    public static void showDialogTipUserGoToAppSettting(final Activity activity, String permissName) {
        AlertDialog dialog = new AlertDialog.Builder(activity)
                .setTitle(permissName+"权限不可用")
                .setMessage("请在-应用设置-权限-中，允许使用"+permissName+"权限")
                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 跳转到应用设置界面
                        goToAppSetting(activity);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        activity.finish();
                    }
                }).setCancelable(false).show();

        // 将对话框的大小按屏幕大小的百分比设置
        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth() * 0.9); //设置宽度
        dialog.getWindow().setAttributes(lp);
    }

    // 跳转到当前应用的设置界面
    private static void goToAppSetting(Activity activity) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
        intent.setData(uri);
        activity.startActivityForResult(intent, 123);
    }
}
